package com.das.mechanic_base.mvp.view.create.fragment;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.kproduce.roundcorners.RoundRelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class X3AddServiceWorkAdapter extends RecyclerView.Adapter<X3AddServiceWorkAdapter.X3AddServiceWorkHolder> {

    private List<SelectWorkBean.ServiceBaseEntityBean> mList;

    private List<SelectWorkBean.ServiceBaseEntityBean> sList;

    private Context mContext;

    private boolean isDetection;

    public X3AddServiceWorkAdapter(Context mContext,List<SelectWorkBean.ServiceBaseEntityBean> mList,boolean isDetection) {
        this.mContext = mContext;
        this.mList = mList;
        this.isDetection = isDetection;
        this.sList = new ArrayList<>();
    }

    public X3AddServiceWorkAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeAddService(List<SelectWorkBean.ServiceBaseEntityBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void changeAlreadyAddService(List<SelectWorkBean.ServiceBaseEntityBean> sList) {
        this.sList = sList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3AddServiceWorkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3AddServiceWorkHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_add_service_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3AddServiceWorkHolder holder, int position) {

//        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
//        if (position == mList.size() - 1) {
//            layoutParams.topMargin = X3ScreenUtils.dipToPx(12,mContext);
//            layoutParams.bottomMargin = X3ScreenUtils.dipToPx(20,mContext);
//        }else {
//            layoutParams.topMargin = X3ScreenUtils.dipToPx(0,mContext);
//            layoutParams.bottomMargin = 0;
//        }
//        holder.itemView.setLayoutParams(layoutParams);

        if ( !isDetection && isGroupHead(position)){
            holder.v_padding.setVisibility(View.GONE);
        }else{
            holder.v_padding.setVisibility(View.VISIBLE);
        }

        if (sList.contains(mList.get(position))) {
            holder.iv_add.setImageResource(R.mipmap.x3_service_cute_icon);
            holder.iv_add.setTag("cute");
            holder.rl_content.setBackgroundColor(Color.parseColor("#E8E8EB"));
        }else {
            holder.iv_add.setImageResource(R.mipmap.x3_service_add_icon);
            holder.iv_add.setTag("add");
            holder.rl_content.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        holder.tv_name.setText(mList.get(position).name);

        holder.itemView.setOnClickListener(view -> {
            String tag = (String) holder.iv_add.getTag();
            SelectWorkBean.ServiceBaseEntityBean variantList = mList.get(position);
            if ("add".equals(tag)) {
                if (iOnSelectWorkList != null) {

                    iOnSelectWorkList.iOnSelectWork(variantList,true);
                }
                holder.iv_add.setImageResource(R.mipmap.x3_service_cute_icon);
                holder.iv_add.setTag("cute");
                holder.rl_content.setBackgroundColor(Color.parseColor("#E8E8EB"));
            }else {
                if (iOnSelectWorkList != null) {
                    iOnSelectWorkList.iOnSelectWork(variantList,false);
                }
                holder.iv_add.setImageResource(R.mipmap.x3_service_add_icon);
                holder.iv_add.setTag("add");
                holder.rl_content.setBackgroundColor(Color.parseColor("#ffffff"));
            }
        });

    }

    IOnSelectWorkList iOnSelectWorkList;

    public X3AddServiceWorkAdapter setiOnSelectWorkList(IOnSelectWorkList iOnSelectWorkList) {
        this.iOnSelectWorkList = iOnSelectWorkList;
        return this;
    }

    public interface IOnSelectWorkList {
        void iOnSelectWork(SelectWorkBean.ServiceBaseEntityBean serviceBean,boolean isAdd);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class X3AddServiceWorkHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_add;
        View v_padding;
        RoundRelativeLayout rl_content;

        public X3AddServiceWorkHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_add = itemView.findViewById(R.id.iv_add);
            v_padding = itemView.findViewById(R.id.v_padding);
            rl_content = itemView.findViewById(R.id.rl_content);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).getSortLetters();

        return   sortLetters;
    }

    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

}
